import syscall

var (pid, err) = try syscall.fork()
if err.has {
    return print('fork err: ', err.msg, '\n')
}

if pid > 0 {
    // 主进程, wait 阻塞, 并读取返回值
    var (result, status) = syscall.wait(pid, 0)
    print('wait status: ', status, '\n')
} else {
    // 子进程通过 exec 调用 sleep
    syscall.exec('/bin/sleep', ['sleep', '1'], ['env1=v', 'env2=2'])
}


// 测试主动 kill
(pid, err) = try syscall.fork()
if err.has {
    return print('fork err: ', err.msg, '\n')
}

if pid > 0 {
    // 主进程, wait 阻塞, 并读取返回值
    var err = try syscall.kill(pid, syscall.SIGKILL)
    if err.has {
        print('kill err: ', err.msg, '\n')
    } else {
        println('kill success')
    }
} else {
    syscall.exec('/bin/sleep', ['sleep', '1000'], [])
}